Notebook Tour


In [5]:
from IPython.display import display, Image, HTML
from talktools import website, nbviewer

The Jupyter Notebook is a web-based application that enables users to create documents that combine live code wth narrative next, equations, images, visualizations and HTML/JavaScript widgets.

This notebook gives an overview of the Jupyter Notebook and the standard IPython Kernel for running Python code.

Interactive exploration

First and foremost, Jupyter is an interactive environment for writing and running code. We provide features to make this as pleasant as possible.


In [2]:
2+2


Out[2]:
4

In [3]:
import math

In [4]:
math.atan?

Inline plotting:


In [5]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [6]:
plot(rand(50))


Out[6]:
[<matplotlib.lines.Line2D at 0x10616f510>]

Seamless access to the system shell:


In [8]:
!ls -al


total 3064
drwxr-xr-x  35 bgranger  staff    1190 Mar 20 19:03 .
drwxr-xr-x  50 bgranger  staff    1700 Mar 14 16:32 ..
-rw-r--r--@  1 bgranger  staff    8196 Mar 18 10:03 .DS_Store
drwxr-xr-x  15 bgranger  staff     510 Mar 20 11:10 .git
-rw-r--r--@  1 bgranger  staff     575 Mar 14 09:48 .gitignore
drwxr-xr-x  18 bgranger  staff     612 Mar 19 15:04 .ipynb_checkpoints
-rw-r--r--   1 bgranger  staff    2840 Mar 20 17:16 Bash Examples.ipynb
-rw-r--r--   1 bgranger  staff  430125 Mar 14 13:09 Components.ipynb
-rw-r--r--   1 bgranger  staff    4833 Mar 20 17:17 Deploying and Scaling.ipynb
-rw-r--r--   1 bgranger  staff    1033 Mar 16 10:46 Future.ipynb
-rw-r--r--   1 bgranger  staff    1838 Mar 20 17:20 Index.ipynb
-rw-r--r--   1 bgranger  staff    9696 Mar 20 17:17 Interaction.ipynb
-rw-r--r--   1 bgranger  staff    6210 Mar 20 17:18 Jupyter and IPython.ipynb
-rw-r--r--   1 bgranger  staff    1082 Feb 19 12:54 LICENSE
-rw-r--r--   1 bgranger  staff   37679 Mar 20 19:03 Notebook Tour.ipynb
-rw-r--r--   1 bgranger  staff   17777 Mar 20 17:19 Notebook Usage.ipynb
-rw-r--r--   1 bgranger  staff    2430 Mar 20 17:19 R Examples.ipynb
-rw-r--r--@  1 bgranger  staff     778 Feb 19 12:54 README.md
-rw-r--r--   1 bgranger  staff    4734 Mar 14 13:24 Theme.ipynb
-rw-r--r--   1 bgranger  staff  411738 Mar 14 13:09 Visualization.ipynb
-rw-r--r--   1 bgranger  staff  535827 Feb 19 13:05 What is Data Science.ipynb
-rw-r--r--   1 bgranger  staff    1130 Mar 19 15:27 courses.csv
drwxr-xr-x   4 bgranger  staff     136 Feb 19 12:54 data
-rw-r--r--   1 bgranger  staff    1676 Feb 19 12:54 frontmatter.py
drwxr-xr-x  59 bgranger  staff    2006 Mar 20 11:13 images
-rw-r--r--@  1 bgranger  staff    1192 Feb 19 13:16 ipythonproject.py
-rw-r--r--   1 bgranger  staff    1836 Feb 19 13:17 ipythonproject.pyc
drwxr-xr-x  18 bgranger  staff     612 Feb 19 13:16 ipythonteam
-rw-r--r--   1 bgranger  staff     606 Feb 19 12:54 load_style.py
-rw-r--r--   1 bgranger  staff    1327 Feb 19 12:54 lorenz.py
-rw-r--r--   1 bgranger  staff    1850 Feb 19 12:54 lorenz.pyc
-rw-r--r--   1 bgranger  staff     972 Feb 19 12:54 talk.css
-rw-r--r--@  1 bgranger  staff    1090 Feb 19 12:54 talktools.py
-rw-r--r--   1 bgranger  staff    1554 Feb 19 13:14 talktools.pyc
-rw-r--r--   1 bgranger  staff      13 Feb 19 12:54 testing.md

Narrative text and equations

In addition to code cells, the Notebook offers Markdown cells, which enable the user to create narrative text with embedded LaTeX equations. Here is a cell that includes Maxwell's equations:

\begin{aligned} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{aligned}

Markdown cells enable users to create complex narratives that tell stories using code and data.

We are calling this literate computing as it is similar to Knuth's literate programming, but involves live code and data.

Rich output

Programming langauges, including Python, allow the writing of textual output to stdout and stderr. Jupyter and IPython extend this idea and allows objects to declare rich output representations:

  • JavaScript
  • HTML
  • LaTeX
  • PDF
  • PNG/JPEG
  • SVG

In IPython, the display function is like print for these rich representations:


In [9]:
from IPython.display import display

Images

The Image object has a JPEG/PNG representation that is rendered by the Notebook:


In [10]:
from IPython.display import Image

In [11]:
i = Image("images/jupyter_logo.png")

This representation is displayed if the object is returned from an expression:


In [12]:
print(i)


<IPython.core.display.Image object>

In [13]:
i


Out[13]:

Or you can manually display the object using display:


In [14]:
display(i)


HTML

The HTML object has an HTML representation:


In [15]:
from IPython.display import HTML

In [16]:
s = """<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>"""

In [17]:
h = HTML(s)

In [18]:
display(h)


Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

JavaScript

The Javascript object has a "representation" that runs JavaScript code in the context of the Notebook.


In [ ]:
from IPython.display import Javascript

In [ ]:
display(Javascript("alert('hi');"))

LaTeX

This display architecture also understands objects that have a LaTeX representation. This is best illustrated by SymPy, which is a symbolic mathematics package for Python.


In [1]:
from __future__ import division
from sympy import *
x, y, z = symbols("x y z")
init_printing(use_latex='mathjax')

When a symbolic expression is passed to display or returned from an expression, the LaTeX representation is computed and displayed in the Notebook:


In [2]:
Rational(3,2)*pi + exp(I*x) / (x**2 + y)


Out[2]:
$$\frac{3 \pi}{2} + \frac{e^{i x}}{x^{2} + y}$$

In [3]:
(1/cos(x)).series(x, 0, 12)


Out[3]:
$$1 + \frac{x^{2}}{2} + \frac{5 x^{4}}{24} + \frac{61 x^{6}}{720} + \frac{277 x^{8}}{8064} + \frac{50521 x^{10}}{3628800} + \mathcal{O}\left(x^{12}\right)$$

nbviewer

nbviewer is a website for sharing Jupyter Notebooks on the web. It uses nbconvert to create a static HTML rendering of any notebook on the internet. This makes it easy to share notebooks with anyone in the world, without their having to install, or even know anything about, Jupyter or IPython.


In [6]:
website('https://nbviewer.jupyter.org')


Out[6]: